/**
 * weapp-js v1.7.1(2021-4-16)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2021 Motigo, Inc.
 * 
 */
Component({
    data: {
        visible: false,
        modal_visible: false,
        image_preview_visible: false,
        login_visible: false
    },
    methods: {
        stopEvent() {},
        onHide() {
            this.setData({
                visible: false
            })
        },
        showModal(opts) {
            let complete_cb = opts.complete;
            opts.complete = () => {
                this.setData({
                    modal_visible: false,
                    visible: false
                });
                complete_cb && complete_cb();
            };

            this.setData({
                visible: true,
            });


            setTimeout(() => {
                this.setData({
                    modal_visible: true
                });
                const node = this.selectComponent('#__weapp_ui_modal__');
                if (node) {
                    node.showModal(opts);
                } else {
                    wx.showModal(opts);
                }
            }, 10);
        },
        previewImage(opts) {
            this.setData({
                visible: true,
            });

            let complete_cb = opts.complete;
            opts.complete = () => {
                this.setData({
                    image_preview_visible: false,
                    visible: false
                });
                complete_cb && complete_cb();
            };

            wx.nextTick(() => {
                this.setData({
                    image_preview_visible: true
                });
                const node = this.selectComponent('#__weapp_ui_image_preview__');
                if (node) {
                    node.previewImage(opts);
                } else {
                    wx.previewImage(opts);
                }
            });
        },
        login(opts) {
            this.setData({
                visible: true,
            });

            return new Promise((resolve, reject) => {
                wx.nextTick(() => {
                    this.setData({
                        login_visible: true
                    });

                    const node = this.selectComponent('#__weapp_ui_login__');

                    if (node) {
                        node.login(opts).then(data => {
                            this.setData({
                                visible: false,
                                login_visible: false
                            });
                            resolve(data);
                        }, err => {
                            reject(err);
                            this.setData({
                                visible: false,
                                login_visible: false
                            });
                        });
                    } else {
                        this.setData({
                            visible: false,
                            login_visible: false
                        });
                        reject();
                    }
                });
            });
        }
    }
})
